package com.ecube.mqtt.page.listener;

import com.ecube.mqtt.page.listener.chargelistener.*;
import com.ecube.mqtt.page.listener.pilechargelistener.*;
import com.ecube.mqtt.page.listener.swapListener.SwapStartResultPageListener;
import com.ecube.mqtt.utils.CommonConstant;
import com.ecube.mqtt.utils.PropertyUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class PileChargeListener implements ActionListener {
    private JButton chargeButton;
    private JFrame first;
    private String path;
    public PileChargeListener(JFrame first, JButton chargeButton, String path) {//获取登录界面、按钮对象
        this.first = first;
        this.path = path;
        this.chargeButton = chargeButton;
    }


    public void actionPerformed(ActionEvent e) {
        first.dispose();
        Dimension dim2 = new Dimension(100, 30);
        Dimension dim3 = new Dimension(300, 30);
        Font font = new Font("宋体",Font.PLAIN,18);

        //生成新界面
        JFrame charge = new JFrame();
        charge.setSize(500, 840);
        charge.setTitle("充电桩充电");//设置窗体标题
        charge.setDefaultCloseOperation(2);
        charge.setLocationRelativeTo(null);
        charge.setFont(font);  //宋体，正常风格，14号字体
        //实例化FlowLayout流式布局类的对象，指定对齐方式为居中对齐组件之间的间隔为10个像素
        FlowLayout fl = new FlowLayout(FlowLayout.RIGHT,30,30);
        //实例化流式布局类的对象
        charge.setLayout(fl);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel labname = new JLabel("环境：");
        labname.setFont(font);
        //将labname标签添加到窗体上
        charge.add(labname);
        String envfirst = "";
        try {
            envfirst = PropertyUtil.getProperties("env", CommonConstant.PILE_CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        JComboBox env = new JComboBox();
        env.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        env.setFont(font);
        env.addItem("DEV");
        env.addItem("FAT");
        env.addItem("UAT");
        env.addItem("HG");
        env.setSelectedItem(envfirst);
        //env.getSelectedItem().toString()
        charge.add(env);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel gunname = new JLabel("枪ID：");
        gunname.setFont(font);
        //将labname标签添加到窗体上
        charge.add(gunname);


        String gunidfirst = "";
        try {
            gunidfirst = PropertyUtil.getProperties("gunid", CommonConstant.PILE_CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField gunid = new JTextField(gunidfirst);
        gunid.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        gunid.setFont(font);
        //将textName标签添加到窗体上
        charge.add(gunid);



        //实例化JLabel标签对象，该对象显示“账号”
        JLabel gunnum = new JLabel("枪编号：");
        gunnum.setFont(font);
        //将labname标签添加到窗体上
        charge.add(gunnum);


        String guncodefirst = "";
        try {
            guncodefirst = PropertyUtil.getProperties("guncode", CommonConstant.PILE_CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        System.out.println(guncodefirst);
        JComboBox guncode = new JComboBox();
        guncode.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        guncode.setFont(font);


        guncode.addItem("A枪");
        guncode.addItem("B枪");
        guncode.addItem("AB枪");
        guncode.setSelectedItem(guncodefirst);
        //guncode.setSelectedIndex(1);
        charge.add(guncode);


        //实例化JLabel标签对象，该对象显示“账号”
        JLabel vehicle = new JLabel("车牌：");
        vehicle.setFont(font);
        charge.add(vehicle);


        String vehiclefirst = "";
        try {
            vehiclefirst = PropertyUtil.getProperties("vehicle", CommonConstant.PILE_CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField vehicletext = new JTextField(vehiclefirst);
        vehicletext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        vehicletext.setFont(font);
        charge.add(vehicletext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel vin = new JLabel("车辆vin：");
        vin.setFont(font);
        charge.add(vin);


        String vinfirst = "";
        try {
            vinfirst = PropertyUtil.getProperties("vin", CommonConstant.PILE_CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField vintext = new JTextField(vinfirst);
        vintext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        vintext.setFont(font);
        charge.add(vintext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel time = new JLabel("充电时长min：");
        time.setFont(font);
        charge.add(time);

        //实例化JTextField标签对象化
        JTextField timetext = new JTextField("200");
        timetext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        timetext.setFont(font);
        charge.add(timetext);

        //实例化JLabel标签对象，该对象显示“账号”
        JLabel soc = new JLabel("电量soc：");
        soc.setFont(font);
        charge.add(soc);

        String socfirst = "";
        try {
            socfirst = PropertyUtil.getProperties("soc", CommonConstant.PILE_CHARGE_CONFIG_PATH);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }
        //实例化JTextField标签对象化
        JTextField soctext = new JTextField(socfirst);
        soctext.setPreferredSize(dim3);//设置除顶级容器组件以外其他组件的大小
        soctext.setFont(font);
        charge.add(soctext);



        //实例化JButton组件
        JButton startbutton = new JButton();
        //设置按键的显示内容
        startbutton.setText("    启动充电    ");
        startbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        startbutton.setSize(dim3);
        charge.add(startbutton);

        PileChargeStartListener start = new PileChargeStartListener(charge,
                env,
                gunid,
                guncode,
                vehicletext,
                vintext,
                soctext,
                path);
        startbutton.addActionListener(start);

        //充电不鉴权
        JButton bmsbutton = new JButton();
        //设置按键的显示内容
        bmsbutton.setText("     BMS报文    ");
        bmsbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        bmsbutton.setSize(dim3);
        charge.add(bmsbutton);

        PileBMSListener bms = new PileBMSListener(charge,
                env,
                gunid,
                guncode,
                vehicletext,
                vintext,
                soctext,
                path);
        bmsbutton.addActionListener(bms);

        //鉴权
        JButton authbutton = new JButton();
        //设置按键的显示内容
        authbutton.setText("    鉴权请求    ");
        authbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        authbutton.setSize(dim3);
        charge.add(authbutton);

        PileAuthListener auth = new PileAuthListener(charge,
                env,
                gunid,
                guncode,
                vehicletext,
                vintext,
                soctext,
                path);
        authbutton.addActionListener(auth);

        //实例化JButton组件
        JButton chargeStartEventButton = new JButton();
        //设置按键的显示内容
        chargeStartEventButton.setText("  充电事件-开始 ");
        chargeStartEventButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        chargeStartEventButton.setSize(dim3);
        charge.add(chargeStartEventButton);

        PileChargeStartEventListener chargeStartEvent = new PileChargeStartEventListener(charge,
                env,
                gunid,
                guncode,
                vehicletext,
                vintext,
                soctext,
                path);
        chargeStartEventButton.addActionListener(chargeStartEvent);

        //实例化JButton组件
        JButton statebutton = new JButton();
        //设置按键的显示内容
        statebutton.setText("    实时状态    ");
        statebutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        statebutton.setSize(dim3);
        charge.add(statebutton);

        PileChargeStateListener state = new PileChargeStateListener(charge,
                env,
                gunid,
                guncode,
                vintext,
                soctext);
        statebutton.addActionListener(state);

        //实例化JButton组件
        JButton endbutton = new JButton();
        //设置按键的显示内容
        endbutton.setText("    结束充电    ");
        endbutton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        endbutton.setSize(dim3);
        charge.add(endbutton);

        PileChargeEndListener end = new PileChargeEndListener(charge,
                env,
                gunid,
                guncode);
        endbutton.addActionListener(end);

        //实例化JButton组件
        JButton stopEventButton = new JButton();
        //设置按键的显示内容
        stopEventButton.setText(" 充电事件-停止  ");
        stopEventButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        stopEventButton.setSize(dim3);
        charge.add(stopEventButton);

        PileChargeStopEventListener stopEvent = new PileChargeStopEventListener(charge,
                env,
                gunid,
                guncode);
        stopEventButton.addActionListener(stopEvent);


        //实例化JButton组件
        JButton chargeResultButton = new JButton();
        //设置按键的显示内容
        chargeResultButton.setText("    充电记录    ");
        chargeResultButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        chargeResultButton.setSize(dim3);
        charge.add(chargeResultButton);

        PileChargeResultListener result = new PileChargeResultListener(charge,
                env,
                gunid,
                guncode,
                vehicletext,
                vintext,
                timetext);
        chargeResultButton.addActionListener(result);


        //实例化JButton组件
        JButton startResultButton = new JButton();
        //设置按键的显示内容
        startResultButton.setText("  启动结果模拟  ");
        startResultButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        startResultButton.setSize(dim3);
        charge.add(startResultButton);

        PileChargeStartResultPageListener pileChargeStartResult = new PileChargeStartResultPageListener(charge,
                env,
                gunid,
                guncode,
                path);
        startResultButton.addActionListener(pileChargeStartResult);

        //实例化JButton组件
        JButton chgStateButton = new JButton();
        //设置按键的显示内容
        chgStateButton.setText("   充电枪状态   ");
        chgStateButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        chgStateButton.setSize(dim3);
        charge.add(chgStateButton);

        PileChgStatePageListener pileChgStateResult = new PileChgStatePageListener(charge,
                env,
                gunid,
                guncode,
                path);
        chgStateButton.addActionListener(pileChgStateResult);

        //实例化JButton组件
        JButton rateButton = new JButton();
        //设置按键的显示内容
        rateButton.setText("    费率下发    ");
        rateButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        rateButton.setSize(dim3);
        charge.add(rateButton);

        PileRateListener rateResult = new PileRateListener(charge,
                env,
                gunid,
                guncode,
                vehicletext,
                vintext,
                soctext,
                path);
        rateButton.addActionListener(rateResult);

        //实例化JButton组件
        JButton chargeReturnFirstPageButton = new JButton();
        //设置按键的显示内容
        chargeReturnFirstPageButton.setText("    返回首页    ");
        chargeReturnFirstPageButton.setFont(new Font("宋体",Font.PLAIN,18));
        //设置按键大小
        chargeReturnFirstPageButton.setSize(dim3);
        charge.add(chargeReturnFirstPageButton);



        BackListener chargeBack = new BackListener(charge, path);
        chargeReturnFirstPageButton.addActionListener(chargeBack);

        charge.setResizable(false);
        charge.setVisible(true);
    }
}